package com.example.mediaplayer;

import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;

public class SpectrumData {
    private final double rate = 0.6;
    private final double width = 4.0;
    private final int rectangle_num = 80;
    private final double radius = 100;
    private final double shiftX = 327;
    private final double shiftY = 241;
    private final double changeAngle =(double)360/rectangle_num;
    public Rectangle[] rectangles = new Rectangle[100];

    public int getRectangle_num(){
        return rectangle_num;
    }

    public double getRadius() {
        return radius;
    }

    public double getShiftX() {
        return shiftX;
    }

    public double getShiftY() {
        return shiftY;
    }

    private double convertHeight(float value){
        float f=Math.abs(value);
        float change=100-(100/(60/f));
        double sqrt = Math.sqrt((1000 / 6.0) * value + 10000);
        return (change)*2;
//        return 80+value;
    }

    public void updateRectangles(float[] magnitudes) {
        for(int i=0;i<rectangle_num;i++){
            rectangles[i].setHeight(convertHeight(magnitudes[i]) * rate);
        }
    }

    public void init(){
        for(int i=0;i<rectangle_num;i++){
            rectangles[i].setHeight(0);
        }
    }

    public SpectrumData(){}

    public SpectrumData(Pane pane){
        for(int i=0;i<rectangle_num;i++) {
            double x = shiftX + radius * Math.cos(Math.PI * 2 * i / rectangle_num);
            double y = shiftY + radius * Math.sin(Math.PI * 2 * i / rectangle_num);
            rectangles[i] = new Rectangle();
            rectangles[i].setLayoutX(x);
            rectangles[i].setLayoutY(y);
            rectangles[i].setWidth(width);
            rectangles[i].setHeight(0);
            //new Stop(0, Color.valueOf("#01BDCD")),new Stop(1,Color.valueOf("#0D29F"))
            //new Stop(0,Color.valueOf("#667EEA")),new Stop(1,Color.valueOf("674BA2")) #5C54AA
            //
            //new Stop(0, Color.valueOf("#5C54AA")),new Stop(0.4,Color.valueOf("#5C54AA")),new Stop(1,Color.valueOf("#1D4BA4"))
            Stop[] stops = new Stop[]{new Stop(0.4,Color.valueOf("#B6A2D7")),new Stop(1,Color.valueOf("#667EEA"))};
            LinearGradient lg1 = new LinearGradient(0, 0, 1, 1, true, CycleMethod.NO_CYCLE, stops);
            rectangles[i].setFill(lg1);
            //            rectangles[i].setRotate(90+changeAngle * i);
            Rotate rotate = new Rotate(270 + changeAngle * i,width/2,0);
            rectangles[i].getTransforms().add(rotate);
            pane.getChildren().add(rectangles[i]);
        }
    }



}
